#! /bin/bash

# huanlf@gmail.com 2007-06-06
# this script is used to rsync files from or to remote computer

# change following values
pidfile='/var/run/rsync_ssh.pid'
logdir='/var/log/rsync'
logfile="`date +%F`"
port='21'
from='remote_host:src_dir'
to='dest_dir'
# from='local_dir'
# to='remote_host:dest_dir'
user='username_on_remote_server'
## we need ssh private key here
priv_key='path_to_ssh_private_key'
ssh_opt='-o StrictHostKeyChecking no'


trap "rm $pidfile" 2 3 15
[ -d "$logdir" ] || mkdir -p  "$logdir"

if [ -e "$pidfile" ]; then
    pid="$( cat "$pidfile" )"
    echo $pid
fi

if [ -n "$pid" ]; then
    kill -0 $pid 2>/dev/null && exit 1
fi

echo $$ > "$pidfile"

rsync -avz --progress --delete -e \
    "ssh -l $user -p $port -i $priv_key $ssh_opt" \
    $from $to > "$logdir/$logfile" 2>&1 

rm $pidfile
